
function syncFolder()
{
	sourceFolder=$1
	targetFolder=$2
	logfile=$3
    rsync -au --log-file=$logfile.temp $sourceFolder $targetFolder
	cat $logfile.temp | grep -v f...p... | grep -v .d...p... >>$logfile
	rm $logfile.temp
}

pid=$$
if [ -d /Volumes/fishSSD ];then

	# 创建 pid 文件，防止多进程冲突
	pidfile=/Volumes/fishSSD/syncing.pid
	logfile=/Volumes/fishSSD/sync.log
	if [ -f $pidfile ];then
		echo `date "+%Y-%m-%d %H:%M:%S"` "sync pid exists, quiting..." >&2
		exit 1;
	fi
	touch $pidfile
	echo $pid >$pidfile
	echo `date "+%Y-%m-%d %H:%M:%S"` "start syncing..."

	# 同步
	syncFolder /Users/yuzhiyuan/WorkSpace/工作事务文档/专利/ /Volumes/fishSSD/专利 $logfile

	# 成功后删除 pid 文件
	pidinfile=$(head -n 1 $pidfile)
	if [ $pid == $pidinfile ];then
		echo `date "+%Y-%m-%d %H:%M:%S"` "deleting pid file"
		rm $pidfile
	fi

else
	echo `date "+%Y-%m-%d %H:%M:%S"` "volume not plugged in, quiting..."
fi